//
//  PrefixHeader.swift
//  testDemo
//
//  Created by frank on 2021/2/25.
//  Copyright © 2021 Alexey Naumov. All rights reserved.
//

import Foundation
import UIKit



func RGBCOLOR(_ r:CGFloat,_ g:CGFloat,_ b:CGFloat) -> UIColor? {
    return UIColor(red: (r)/255.0, green: (g)/255.0, blue: (b)/255.0, alpha: 1.0)
}

func RGBACOLOR(_ r:CGFloat,_ g:CGFloat,_ b:CGFloat, _ a:CGFloat) -> UIColor? {
    return UIColor(red: (r)/255.0, green: (g)/255.0, blue: (b)/255.0, alpha: (a))
}

func HEXRGBCOLOR(_ h:Int) -> UIColor? {
    return RGBCOLOR((CGFloat)((h>>16)&0xFF), (CGFloat)((h>>8)&0xFF), (CGFloat)(h&0xFF))
}

func HEXRGBACOLOR(_ h:Int,a:CGFloat) -> UIColor? {
    return RGBACOLOR((CGFloat)((h>>16)&0xFF), (CGFloat)((h>>8)&0xFF), (CGFloat)(h&0xFF),a)
}

func MIN(_ a:CGFloat,_ b:CGFloat) -> CGFloat {
    return a < b ? a : b
}

func MAX(_ a:CGFloat,_ b:CGFloat) -> CGFloat {
    return a > b ? a : b
}

func Size(_ x:CGFloat) -> CGFloat {
    return ((x)*MIN(kCNScreenW,kCNScreenH)/375.0)
}


public var isPhoneXAbove: Bool {
    return KBottomHomeHeight > 0
}
//获取当前操作系统的版本
var System_Version : Float = Float(UIDevice.current.systemVersion) ?? 0.0

let AppDelegateInstance = UIApplication.shared.delegate
let kCNScreenW : CGFloat = UIScreen.main.bounds.size.width
let kCNScreenH : CGFloat = UIScreen.main.bounds.size.height

/// 信号栏高度
/// - Returns: 高度
public func kStatusHeight() ->CGFloat {
    if #available(iOS 13.0, *){
        return getWindow()?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
    }else{
        return UIApplication.shared.statusBarFrame.height
    }
}
///获取当前设备window用于判断尺寸
public func getWindow() -> UIWindow?{
    if #available(iOS 13.0, *){
        let winScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
        return winScene?.windows.first
    }else{
        return UIApplication.shared.keyWindow
    }
}

/// 导航栏高度 实时获取,可获取不同分辨率手机横竖屏切换后的实时高度变化
/// - Returns: 高度
public func kNavBarHeight() ->CGFloat {
    return UINavigationController().navigationBar.frame.size.height
}

/// 获取屏幕导航栏+信号栏总高度
public let KNavAndStatusHeight = kStatusHeight() + kNavBarHeight()
/// 获取刘海屏底部home键高度,普通屏为0
public let KBottomHomeHeight = getWindow()?.safeAreaInsets.bottom ?? 0

/// TabBar高度 实时获取,可获取不同分辨率手机横竖屏切换后的实时高度变化
/// - Returns: 高度
public func kTabBarHeight() ->CGFloat {
    return UITabBarController().tabBar.frame.size.height
}
//刘海屏=TabBar高度+Home键高度, 普通屏幕为TabBar高度
public let kTabAndBottomHeight = kTabBarHeight() + KBottomHomeHeight


// MARK:- 字体
/// 系统默认字体
public let Font11 = UIFont.systemFont(ofSize: 11)
/// 系统默认字体
public let Font12 = UIFont.systemFont(ofSize: 12)
/// 系统默认字体
public let Font13 = UIFont.systemFont(ofSize: 13)
/// 系统默认字体
public let Font14 = UIFont.systemFont(ofSize: 14)
/// 系统默认字体
public let Font15 = UIFont.systemFont(ofSize: 15)
/// 系统默认字体
public let Font16 = UIFont.systemFont(ofSize: 16)

///根据屏幕自适应字体参数 16*FontFit
public let FontFit = min(UIScreen.main.bounds.width, UIScreen.main.bounds.height) / 375

/// 系统默认字体
public func SystemFont(_ size: CGFloat) -> UIFont {
    return .systemFont(ofSize: size)
}
/// 系统默认字体
public func SystemFontBold(_ size: CGFloat) -> UIFont {
    return .boldSystemFont(ofSize: size)
}
/// 系统默认字体
public func SystemFont(_ size: CGFloat, weight: UIFont.Weight) -> UIFont {
    return .systemFont(ofSize: size, weight: weight)
}

public enum Weight {
    case medium
    case semibold
    case light
    case ultralight
    case regular
    case thin
}
/// pingfang-sc 字体
public func Font(_ size: CGFloat) -> UIFont {
    return FontWeight(size, weight: .regular)
}
/// pingfang-sc 字体
public func FontMedium(_ size: CGFloat) -> UIFont {
    return FontWeight(size, weight: .medium)
}
/// pingfang-sc 字体
public func FontBold(_ size: CGFloat) -> UIFont {
    return FontWeight(size, weight: .semibold)
}
/// pingfang-sc 字体
public func FontWeight(_ size: CGFloat, weight: Weight) -> UIFont {
    var name = ""
    switch weight {
    case .medium:
        name = "PingFangSC-Medium"
    case .semibold:
        name = "PingFangSC-Semibold"
    case .light:
        name = "PingFangSC-Light"
    case .ultralight:
        name = "PingFangSC-Ultralight"
    case .regular:
        name = "PingFangSC-Regular"
    case .thin:
        name = "PingFangSC-Thin"
    }
    return UIFont(name: name, size: size) ?? UIFont.systemFont(ofSize: size)
}

// MARK: - App信息

/// App 显示名称
public var AppDisplayName: String? {
    return Bundle.main.infoDictionary?["CFBundleDisplayName"] as? String
}

public var AppName: String? {
    return Bundle.main.infoDictionary?[kCFBundleNameKey as String] as? String
}

/// app 的bundleid
public var AppBundleID: String? {
    return Bundle.main.bundleIdentifier
}

/// build号
public var AppBuildNumber: String? {
    return Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as? String
}

/// app版本号
public var AppVersion: String? {
    return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
}

// MARK: 基本颜色
public func localColor(_ named: String) -> UIColor {
    guard let color = UIColor(named: named, in: Bundle.main, compatibleWith: nil) else {
        return UIColor(named: named) ?? UIColor.clear
    }
    return color
}
/// 支持暗黑模式的颜色
let KbaseBlue = localColor( "baseBlue")
let KbaseGray = localColor("baseGray")
let KbaseGreen = localColor("baseGreen")
let KbaseIndigo = localColor("baseIndigo")
let KbaseOrange = localColor("baseOrange")
let KbasePink = localColor("basePink")
let KbasePurple = localColor("basePurple")
let KbaseRed = localColor("baseRed")
let KbaseTeal = localColor("baseTeal")
let KbaseYellow = localColor("baseYellow")
let KbaseBackground = localColor("bgColor")
let KbaseBGColor = localColor("backColor")

let KbaseLine = localColor("baseLine")

let KtextTitleColor = localColor("textTitleColor")
let KtextSecColor = localColor("textSecColor")
let KtextDesColor = localColor("textDesColor")
let KtextLinkColor = localColor("textLinkColor")


let CNformalServerIP = ""
let CNTestServerIP = "http://192.168.1.72:9000/"

public var IsProduction : Bool {
    #if DEBUG
    return false
    #else
    return true
    #endif
}

public var BaseUrl : String? {
    #if DEBUG
    return CNTestServerIP
    #else
    return CNformalServerIP
    #endif
}
public var ApiBaseUrl : String {
    return BaseUrl ?? ""
}


extension UIApplication {
    var currentWindow: UIWindow? {
        if let vc = UIApplication.shared.windows.last?.rootViewController{
            return UIApplication.shared.windows.last!
        }
        if #available(iOS 13.0, *) {
            if let window = connectedScenes
                .filter({$0.activationState == .foregroundActive})
                .map({$0 as? UIWindowScene})
                .compactMap({$0})
                .first?.windows
                .filter({$0.isKeyWindow}).first{
                return window
            }else if let window = UIApplication.shared.delegate?.window{
                return window
            }else{
                return nil
            }
        } else {
            if let window = UIApplication.shared.delegate?.window{
                return window
            }else{
                return nil
            }
        }
    }
}

extension UIViewController {
    // MARK: - 找到当前显示的viewcontroller
    class func current(base: UIViewController?) -> UIViewController? {
        
        if let nav = base as? UINavigationController {
            return current(base: nav.visibleViewController)
        }
        if let tab = base as? UITabBarController {
            return current(base: tab.selectedViewController)
        }
        if let presented = base?.presentedViewController {
            return current(base: presented)
        }
        if let split = base as? UISplitViewController{
            return current(base: split.presentingViewController)
        }
        return base
    }
    

}
